package com.ps.yygh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ps.yygh.MD5;
import com.ps.yygh.model.hosp.HospitalSet;
import com.ps.yygh.result.Result;
import com.ps.yygh.service.HospitalSetService;

import com.ps.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Author jps
 * @Date 2022/8/1 20:32
 */
@RestController
@RequestMapping("/admin/hosp/hospSet")
@Slf4j
@Api(tags = "医院设置接口")
public class HospitalSetController {

    @Autowired(required = false)
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查看医院设置列表")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "根据编号删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteById(
            @ApiParam(name = "id",value = "医院设置编号",required = true)
            @PathVariable("id")Long id
    ){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //参数列表中是请求参数query，查询搭配@GetMapping
    //参数列表中是请求参数body，即为json数据格式，查询搭配@PostMapping
    @ApiOperation(value = "分页条件查看医院设置列表")
    @PostMapping("findAllByPage/{pageNow}/{limit}")
    public Result findAllByPage(
            @ApiParam(name="pageNow",value = "当前页码",required = true) @PathVariable("pageNow")Integer pageNow,
            @ApiParam(name="limit",value = "每页记录数",required = true) @PathVariable("limit")Integer limit,
            @RequestBody HospitalSetQueryVo hospitalSetQueryVo
            ){
        //封装Page对象
        Page<HospitalSet> page = new Page<>(pageNow, limit);

        //封装查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        //获取医院名称
        String hosname = hospitalSetQueryVo.getHosname();
        if (StringUtils.hasLength(hosname)){
            queryWrapper.like("hosname", hosname);
        }
        //获取医院编号
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (StringUtils.hasLength(hoscode)){
            queryWrapper.like("hoscode", hoscode);
        }

        //分页查询
        Page<HospitalSet> pageModel = hospitalSetService.page(page,queryWrapper);
        return Result.ok(pageModel);
    }


    //添加医院设置列表
    @ApiOperation("添加医院设置列表")
    @PostMapping("add")
    public Result add(
           @RequestBody HospitalSet hospitalSet
    ){
        hospitalSet.setStatus(1);

        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



}
